﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //5、注册事件，当在ATM中取走一大笔钱的时候警告，调用alertBigMoney()去处理
            Form1.BigMoneyFetched += Form1.alertBigMoney;
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
         
        
    }
    public class BigMoneyArgs : EventArgs
    {

        public Account account;//账户信息
        public float fetchedmoney;//输入取款金额
    }
    //2、声明委托类型
    public delegate void BigMoneyFetchedEventHandler(object sender, BigMoneyArgs e);


    //自定义异常类型,表示取钱时的出钞异常
    public class BadCashException : ApplicationException
    {
        private int idNumber;
        public BadCashException(String message, int id) : base(message)
        { this.idNumber = id; }

    }
    public class Account
    {
        protected string accountnumber;
        protected string password;
        protected float balanve;

        public Account(string accountnumber,string password)
        {
            this.accountnumber = accountnumber;
            this.password = password;
        }
        //1、声明参数类型，事件对象
    
    //账户类，表示账户的信息


        public Account()
        {

        }
        public float queryBalanve()
        {
            return balanve;
        }
        public void withdraw(float withdrawMoney)
        {
            // 随机生成异常，表示出钞的异常
            Random ran = new Random();
            int a = ran.Next(0, 5);
            if (a < 2) throw new BadCashException("出钞错误，请重试", 222);//抛出异常
            balanve -= withdrawMoney;
        }
        public void deposit(float depositMoney)
        {
            balanve += depositMoney;
        }
        //set get 函数
        public string getaccountnumber() { return accountnumber; }
        public void setaccountnumber(string value) { this.accountnumber = value; }
        public string getpassword() { return password; }
        public void setpassword(string value) { this.password = value; }


    }
    public class CreditAccount : Account
    {
        private string creditline;
        public CreditAccount(string accountnumber, string password,string creditline)
        {
            this.accountnumber = accountnumber;
            this.password = password;
            this.creditline = creditline;
        }
        public string getcreditline() { return creditline; }
        public void setcreditline(string creditline) { this.creditline = creditline; }
    }
    //银行类
    public class Bank
    {
        static List<Account> accountsList = new List<Account>();//用户列表，静态数据

        //用户注册函数，新建一个Account对象并放入用户列表中
        public static void register(string accountnumber, string password)
        {
            Account newAccount = new Account(accountnumber, password);
            accountsList.Add(newAccount);
        }
        //登录函数，检验输入的账号是否存在以及密码是否正确
        public static Account login(string accountnumber, string password)
        {
            foreach (Account i in accountsList)
            {
                if (i.getaccountnumber() == accountnumber && i.getpassword() == password)
                {
                    return i;
                }
            }
            return null;
        }
        public static List<Account> getaccountsList() { return accountsList; }
    }
    //ATM类，与银行类类似
    public class ATM
    {
        //登录函数，ATM不能注册
        public static Account login(string accountnumber, string password)
        {
            foreach (Account i in Bank.getaccountsList())
            {
                if (i.getaccountnumber() == accountnumber && i.getpassword() == password)
                {
                    return i;
                }
            }
            return null;
        }
    }

}

    

